//
//  HORefreshViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HORefreshViewController.h"
#import "Logger.h"
#import "HOUtils.h"

@interface HORefreshViewController ()
{
}
@end

@implementation HORefreshViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated
{
	[super viewWillAppear:animated];
	
	_refreshFooterView.hidden = YES;
}

- (void)initTableView
{
	[super initTableView];
	[self initEGORefreshTableHeaderView];
	[self initEGORefreshTableFooterView];
}

- (void)initTableViewWithPullUp
{
	[super initTableView];
	[self initEGORefreshTableFooterView];
}

- (NSInteger)getListCount
{
	return [self.tableViewDataList count];
}

- (void)initEGORefreshTableHeaderView
{
	_refreshHeaderView = [[EGORefreshTableView alloc]
						  initWithFrame:CGRectMake(0.0f,
												   -65,
												   baseTableview.frame.size.width,
												   65)
						  withTag:EGOHeaderView];
	_refreshHeaderView.delegate = self;
	[_refreshHeaderView refreshLastUpdatedDate];
	[baseTableview addSubview:_refreshHeaderView];
}

#pragma mark refreshTable
- (void)initEGORefreshTableFooterView
{
    _refreshFooterView = [[EGORefreshTableView alloc]
						  initWithFrame:CGRectMake(0.0f,
												   baseTableview.contentSize.height,
												   baseTableview.frame.size.width,
												   0)
						  withTag:EGOBottomView];
	_refreshFooterView.delegate = self;    //Init Mor
	[_refreshFooterView refreshLastUpdatedDate];
	[baseTableview addSubview:_refreshFooterView];
	_refreshFooterView.hidden = YES;
}

- (void)dealloc
{
    if(_refreshFooterView)
	{
		[HOUtils removeFromSuperViewForViews:_refreshFooterView];
		[_refreshFooterView release];
		_refreshFooterView = nil;
	}
	if(_refreshHeaderView)
	{
		[HOUtils removeFromSuperViewForViews:_refreshHeaderView];
		[_refreshHeaderView release];
		_refreshHeaderView = nil;
	}
    [super dealloc];
}
#pragma mark

#pragma mark EGORefreshTableFooterDelegate Methods
#pragma mark Data Source Loading / Reloading Methods

- (void)updateRefreshViewFrame:(NSInteger)count
{
    _refreshFooterView.frame = CGRectMake(0.0f,  baseTableview.contentSize.height, baseTableview.frame.size.width, count < 20 ? 0 : 65);
}

- (BOOL)EGORefreshTableDataSourceIsLoading:(EGORefreshTableView *)view
{
	return view.tag == EGOHeaderView ? isHeaderLoadingMore : isFooterLoadingMore;
}

- (void)EGORefreshTableDidTriggerRefresh:(EGORefreshTableView*)view
{
	if(view.tag == EGOHeaderView)
	{
		//下拉
		if(isHeaderLoadingMore)
		{
			return;
		}
		isHeaderLoadingMore = YES;
		[self pullDownToRefresh];
	}
	else
	{
		//上啦
		if(isFooterLoadingMore)
		{
			return;
		}
		if([self isGotAllOfData])
		{
			[self doneLoadingTableFooterViewData];
			return;
		}
		NSInteger count = [self getListCount];
		if(count < kPageSize)
		{
			[self doneLoadingTableFooterViewData];
			return;
		}
		isFooterLoadingMore = YES;
		[self pullUpToRefresh];
	}
}

- (NSDate*)EGORefreshTableDataSourceLastUpdated:(EGORefreshTableView *)view
{
    return [NSDate date]; // should return date data source was last changed
    
}

#pragma mark
#pragma mark Done after requested.
-(void)doneLoadingTableFooterViewData
{
    isFooterLoadingMore = NO;
	_refreshFooterView.hidden = YES;
	[_refreshFooterView egoRefreshScrollViewDataSourceDidFinishedLoading:baseTableview];
	_refreshFooterView.frame = CGRectMake(0.0f, baseTableview.contentSize.height, baseTableview.frame.size.width, 0);
	baseTableview.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
}

-(void)doneLoadingTableHeaderViewData
{
	isHeaderLoadingMore = NO;
	_refreshHeaderView.hidden = YES;
	[_refreshHeaderView egoRefreshScrollViewDataSourceDidFinishedLoading:baseTableview];
	_refreshHeaderView.frame = CGRectMake(0.0f, baseTableview.contentSize.height, baseTableview.frame.size.width, 0);
	baseTableview.contentInset = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
}

- (void)doneLoadingTableViewRefresh
{
	[baseTableview reloadData];
	if(isFooterLoadingMore)
	{
		[self doneLoadingTableFooterViewData];
	}
	else if(isHeaderLoadingMore)
	{
		[self doneLoadingTableHeaderViewData];
	}
}
#pragma mark
#pragma mark Pull to refresh

- (void)pullDownToRefresh
{
	//下拉
	[self performSelector:@selector(doneLoadingTableHeaderViewData) withObject:nil afterDelay:1];
}

- (void)pullUpToRefresh
{
	//上拉
	[self performSelector:@selector(doneLoadingTableFooterViewData) withObject:nil afterDelay:1];
}
#pragma mark
#pragma mark UIScrollViewDelegate Methods
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    point = scrollView.contentOffset;
}

- (BOOL)isGotAllOfData
{
    return NO;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //scrollview.offset do not contain headerview
	CGPoint pt = scrollView.contentOffset;
	if (pt.y > 65)
	{
		if (isFooterLoadingMore)
			return;
		//向上提加载更多
		[_refreshFooterView egoRefreshScrollViewDidScroll:scrollView];
	}
	else if (pt.y < 65)
	{
		if (isHeaderLoadingMore)
			return;
		[_refreshHeaderView egoRefreshScrollViewDidScroll:scrollView];
	}
	
	if(pt.y >= 0 && [self getListCount] >= kPageSize)
	{
		_refreshFooterView.frame = CGRectMake(0.0f,
											  baseTableview.contentSize.height,
											  baseTableview.frame.size.width,
											  65);
		_refreshFooterView.hidden = NO;
	}
	else
	{
		_refreshFooterView.frame = CGRectMake(0.0f,
											  baseTableview.contentSize.height,
											  baseTableview.frame.size.width,
											  0);
	}

}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
	CGPoint pt = scrollView.contentOffset;
	if (pt.y > 65)
	{
		[_refreshFooterView egoRefreshScrollViewDidEndDragging:scrollView];
	}
	else if (pt.y < 65)
	{
		[_refreshHeaderView egoRefreshScrollViewDidEndDragging:scrollView];
	}
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
